package repository.impl;

import entity.Patient;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import repository.PatientRepository;

import java.util.List;

@Transactional
@Repository
public class PatientRepositoryImpl implements PatientRepository {
    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }

    public Patient load(Integer id) {
        return (Patient) getCurrentSession().load(Patient.class, id);
    }

    public Patient get(Integer id) {
        return (Patient) getCurrentSession().get(Patient.class, id);
    }

    public List findAll() {
        Query query = getCurrentSession().createQuery("from " + Patient.class.getName());
        return query.list();
    }

    public void persist(Patient entity) {
        getCurrentSession().persist(entity);
    }

    public Integer save(Patient entity) {
        return (Integer) getCurrentSession().save(entity);
    }

    public void saveOrUpdate(Patient entity) {
        getCurrentSession().saveOrUpdate(entity);
    }

    public void delete(Integer id) {
        Patient patient = load(id);
        getCurrentSession().delete(patient);
    }

    public void flush() {
        getCurrentSession().flush();
    }
}
